package io.renren.modules.noc.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.noc.service.NocTemplateService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.noc.entity.NocTemplateColEntity;
import io.renren.modules.noc.service.NocTemplateColService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author whc
 * @email hucheng_86@126.com
 * @date 2019-04-03 22:37:46
 */
@RestController
@RequestMapping("noc/noctemplatecol")
public class NocTemplateColController extends BaseController{
    @Autowired
    private NocTemplateColService nocTemplateColService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("noc:noctemplatecol:list")
    public R list(@RequestParam Map<String, Object> params){
        Integer defalutTemplateId = this.getDefaultTemplateId();
        if(defalutTemplateId==null){
            return R.error("请先设置默认活动！");
        }
        params.put("templateId",defalutTemplateId);
        PageUtils page = nocTemplateColService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 根据模板查询
     */
    @RequestMapping("/listByTid")
    public R listByTid(@RequestParam Map<String, Object> params){
        String tid  = (String)params.get("templateId");
        if(StringUtils.isNotEmpty(tid)){
            params.put("templateId",Integer.parseInt(tid));
            PageUtils page = nocTemplateColService.queryPage(params);
            return R.ok().put("page", page);
        }
        return R.error();
    }

    /**
     * 列表
     */
    @RequestMapping("/listAll")
    //@RequiresPermissions("noc:noctemplatecol:list")
    public R listAll(@RequestParam Map<String, String> params){
        String templateId = params.get("templateId");
        if(StringUtils.isEmpty(templateId)){
            Integer defalutTemplateId = this.getDefaultTemplateId();
            if(defalutTemplateId==null){
                return R.error("请先设置默认活动！");
            }
            params.put("template_id",defalutTemplateId.toString());
        }
        List<Map<String,Object>> list = nocTemplateColService.listAll(params);
        return R.ok().put("list", list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("noc:noctemplatecol:info")
    public R info(@PathVariable("id") Integer id){
        NocTemplateColEntity nocTemplateCol = nocTemplateColService.getById(id);

        return R.ok().put("nocTemplateCol", nocTemplateCol);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("noc:noctemplatecol:save")
    public R save(@RequestBody NocTemplateColEntity nocTemplateCol){
        nocTemplateColService.save(nocTemplateCol);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("noc:noctemplatecol:update")
    public R update(@RequestBody NocTemplateColEntity nocTemplateCol){
        ValidatorUtils.validateEntity(nocTemplateCol);
        nocTemplateColService.updateById(nocTemplateCol);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("noc:noctemplatecol:delete")
    public R delete(@RequestBody Integer[] ids){
        nocTemplateColService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
